<?php

//创建核销记录表
pdo_query("CREATE TABLE IF NOT EXISTS `ims_tg_hexiao` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `uniacid` int(10) unsigned NOT NULL COMMENT '公众账号id',
  `cid` int(10) COMMENT 'collect表id',
  `orderno` varchar(100) COMMENT '核销订单号',
  `openid` varchar(100) NOT NULL COMMENT 'openID',
  `check_openid` varchar(100) NOT NULL COMMENT '核销员openID',
  `check_time` int(11) NOT NULL COMMENT '核销时间',
  PRIMARY KEY (`id`),
  KEY `idx_uniacid` (`uniacid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
");


$orderno = $_GPC['mid'];
$openid = $_W['openid'];
$result = $_GPC['result'];
$checkorder = pdo_fetch("select * from " . tablename('tg_collect') . " where id='{$orderno}' ");
$is_hexiao_member = 1;
$saler = pdo_fetch('select * from ' . tablename('tg_saler') . ' where openid=:openid and status=1', array(':openid' => $openid));
if (empty($saler)) {
    $is_hexiao_member = 0;
}
if ($_W['isajax']) {
    $order = pdo_fetch('select * from ' . tablename('tg_order') . ' where orderno=:orderno', array(':orderno' => $checkorder['orderno']));
    $url = app_url('order/order/detail', array('id' => $order['id']));
    hexiao_success($checkorder['goodsname'], $order['openid'], $checkorder['num'], TIMESTAMP, $url);

    if ($checkorder['active_times'] < 1) {
        wl_json(0, '该项目已服务，请勿重复服务！');
    }
//    if($checkorder['active_times'] == 1){
//        pdo_update('tg_collect',array('check_status'=>1,'check_openid'=>$openid,'check_time'=>TIMESTAMP),array('id'=>$orderno));
//    }
    if (pdo_update('tg_collect', array('active_times' => ($checkorder['active_times'] - 1)), array('id' => $orderno))) {
        $data = array();
        $data['uniacid'] = $checkorder['uniacid'];
        $data['cid'] = $checkorder['id'];
        $data['orderno'] = $checkorder['orderno'];
        $data['openid'] = $checkorder['openid'];
        $data['check_openid'] = $openid;
        $data['check_time'] = TIMESTAMP;
        pdo_insert('tg_hexiao', $data);
        if ($checkorder['active_times'] == 1) {
            pdo_update('tg_collect', array('check_status' => 1), array('id' => $orderno));
            if ($_W['uniacid'] == 5) {
                pdo_update('tg_order',
                    array(
                        'storeid' => $checkorder['storeid'],
                        'hexiaotime' => TIMESTAMP,
                        'gettime' => TIMESTAMP,
                        'is_hexiao' => 1,
                        'veropenid' => $openid
                    ), array(':orderno' => $checkorder['orderno'])
                );
            }
        }

        $check_order = pdo_fetchall("select * from " . tablename('tg_collect') . " where  orderno=:orderno  and check_status = 0", array(':orderno' => $checkorder['orderno']));
        if (count($check_order) == 0) {
            pdo_update('tg_order', array('status' => 3, 'successtime' => TIMESTAMP), array('orderno' => $checkorder['orderno']));
        }
        if ($_W['uniacid'] != 5) {
            pdo_update('tg_member', array('order_times +=' => 1), array('uniacid' => $checkorder['uniacid'], 'openid' => $checkorder['openid']));
        }
        wl_json(1);
    } else {
        wl_json(0, '核销失败，请重试！');
    }
}
include wl_template('check');
exit();